package com.cdream.basic.algorithm.leetcode.slideWindow;

/**
 * @author cdream
 * @date 5/19/21
 */
public class Question209 {
    public int minSubArrayLen(int s, int[] nums) {
        int start = 0;
        int end = -1;
        int minLength = nums.length + 1;
        int sum = 0;

        while (end <= nums.length - 1) {
            if (sum < s) {
                sum += nums[++end];
            } else {
                sum -= nums[start--];
            }
            minLength = Math.min(minLength, end - start);
        }
        if (minLength == nums.length + 1) {
            return 0;
        }
        return minLength;
    }
}
